package com.simpou.commons.persistence.jpa.common;

import com.simpou.commons.model.entity.BaseEntity;
import com.simpou.commons.model.entity.IdentifiableEntity;
import com.simpou.commons.persistence.common.Cache;
import lombok.RequiredArgsConstructor;

import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 * Gerenciador de cache da persistência jpa.
 *
 * @author Jonas Pereira
 * @since 2013-12-14
 */
public class JpaCache implements Cache {

    private final javax.persistence.Cache cache;

    public JpaCache(EntityManager entityManager) {
        this.cache = entityManager.getEntityManagerFactory().getCache();
    }

    @Override
    public <T extends IdentifiableEntity<?>> void evict(final Class<T> entityClass, final Serializable identity) {
        if(cache.contains(entityClass, identity)){
            cache.evict(entityClass, identity);
        }
    }

    @Override
    public void evictAll() {
        cache.evictAll();
    }
}
